﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;


namespace KTX.Tanh
{
    public partial class frmDacQuyen : Form
    {
        SqlConnection conn;
        public frmDacQuyen()
        {
            InitializeComponent();
          
            Configuration _config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var strConn = _config.ConnectionStrings.ConnectionStrings[1].ConnectionString;
            conn = new SqlConnection(strConn);

        }
        
        private void frmDacQuyen_Load(object sender, EventArgs e)
        {
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            var cmd = new SqlCommand("Select * from tb_Privilege", conn);
            var tb = new DataTable();
            tb.Load(cmd.ExecuteReader());
            dataGridView1.DataSource = tb;


        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            try
            {
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                var sql_ins = string.Format("SET IDENTITY_INSERT tb_Privilege on; insert into tb_Privilege(Id, Name) values({0},'{1}');SET IDENTITY_INSERT tb_Privilege off", txtId.Text, txtName.Text);
                var cmd_ins = new SqlCommand(sql_ins, conn);
                cmd_ins.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            try
            {
                var sql_upd = "update [tb_Privilege] set  Name = @name where Id = '"+txtId.Text+"'";
                
                SqlCommand cmd_upd = new SqlCommand(sql_upd, conn);

              
                cmd_upd.Parameters.AddWithValue("@name",txtName.Text);

               // if(MessageBox.Show("Bạn có muốn cập nhật thông tin này ? ","Thông Báo",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.OK);
              cmd_upd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            try
            {
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                var sql_del = "delete from tb_Privilege where Id = '" + txtId.Text + "'";
                SqlCommand cmd_del = new SqlCommand(sql_del, conn);
                if (MessageBox.Show("Bạn có muốn tiếp tục xóa ?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) ;
                cmd_del.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btTaiDuLieu_Click(object sender, EventArgs e)
        {
            try
            {
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                var cmd = new SqlCommand("Select * from tb_Privilege", conn);
                var tb = new DataTable();
                tb.Load(cmd.ExecuteReader());
                dataGridView1.DataSource = tb;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        int dong;
        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try 
            {
                dong = e.RowIndex;
                txtId.Text = dataGridView1.Rows[dong].Cells[0].Value.ToString();
                txtName.Text = dataGridView1.Rows[dong].Cells[1].Value.ToString();

            }
            catch(System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void txtId_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
